<?php
use yii\helpers\Url;

$web = Url::base();
$this->title = '创建新机构';
?>

<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/apply-for-institution.css"/>
<style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }

    html {
        font-size: 20px;
    }

    .await-window .shade {
        width: 100%;
        height: 100%;
        background: rgba(255, 255, 255, 0);
        position: fixed;
        left: 0;
        top: 0;
        z-index: 999;
    }

    .await-window .window-content {
        width: 6.25rem;
        height: 6.25rem;
        border-radius: .25rem;
        background: rgba(0, 0, 0, .7);
        position: fixed;
        top: 50%;
        left: 50%;
        margin-left: -3.125rem;
        margin-top: -3.125rem;
        z-index: 9999;
    }

    @keyframes animate1 {
        100% {
            transform: rotate(360deg);
        }
    }

    @-webkit-keyframes animate1 {
        100% {
            transform: rotate(360deg);
        }
    }

    .await-window .window-content img {
        width: 1.5rem;
        height: 1.5rem;
        margin-left: 2.375rem;
        margin-top: 1.6rem;
        animation: animate1 2s linear infinite;
        -webkit-animation: animate1 1s linear infinite;
    }

    .await-window .window-content p {
        font-size: .55rem;
        font-family: "微软雅黑";
        color: #FFFFFF;
        text-align: center;
        margin-top: 1rem;
    }
</style>

<div class="main">
    <form method="post" action="apply-for-institution-hy-form">
        <div class="frame">
            <span class="verification-code">企业全称：</span>
            <input type="text" name="txtName" id="txtName">
        </div>
        <div class="hr"></div>
        <div class="frame">
            <span class="verification-code">联系人&nbsp;&nbsp;&nbsp;&nbsp;：</span>
            <input type="text" name="txtContacter" id="txtContacter" maxlength="30">
        </div>
        <div class="frame">
            <script>
                function tel(v) {
                    var myreg = /^(\d{11})$/;
                    if (myreg.test(v) == false) {
                        alert('请输入有效的手机号码！');
                        return false;
                    }
                    return true;
                }
            </script>
            <span class="verification-code">联系手机：</span>
            <input type="tel" maxlength="11" name="txtContacterPhone" id="txtPhone">
        </div>
        <div class="frame" style="border: none">
            <span class="verification-code">验证码&nbsp;&nbsp;&nbsp;&nbsp;：</span>
            <input type="text" style="width: 5rem" name="txtVerificationCode" id="txtVerificationCode">

            <div class="get-verification-code" id="btnSmsVerificationCode">
                获取验证码<span class="get-verification-time"></span>
            </div>
            <script>
                $('#btnSmsVerificationCode').click(function () {
                    if ($(this).hasClass('get-verification-code-disabled')) {
                        return;
                    }

                    $(this).addClass('get-verification-code-disabled');
                    $(this).text('正在获取...');
                    var phone = $('#txtPhone').val().trim();
                    if (phone.length == 0) {
                        alert('请输入手机号码');
                        $(this).text('获取验证码');
                        $(this).removeClass('get-verification-code-disabled');
                        $('#txtPhone').focus();
                        return;
                    }
                    var that = this;
                    requestJson('send-sms-verification-code-handler', {phone: phone}, true, function (responseData) {
                        if (responseData.status != 0) {
                            $(that).text('获取验证码');
                            $(that).removeClass('get-verification-code-disabled');
                            alert(responseData.message);
                        } else {
                            verifyTimeoutValue = 60;
                            verifyTimeout();
                        }
                    }, function () {
                        $(that).text('获取验证码');
                        $(that).removeClass('get-verification-code-disabled');
                    });
                });

                var verifyTimeoutValue = 0;
                function verifyTimeout() {
                    if (verifyTimeoutValue > 0) {
                        $('#btnSmsVerificationCode').text('重新获取(' + verifyTimeoutValue + ')');
                        verifyTimeoutValue--;
                        setTimeout(verifyTimeout, 1000);
                    } else {
                        $('#btnSmsVerificationCode').text('获取验证码');
                        $('#btnSmsVerificationCode').removeClass('get-verification-code-disabled');
                    }
                }
            </script>
        </div>
        <div class="frame-address" style="height: 3rem;">
            <span class="verification-code">联系地址：</span>
            <textarea name="txtAddress" id="txtAddress" maxlength="100"></textarea>
        </div>
        <input type="button" id="btnSubmit" class="apply-button" value="申请使用">
    </form>
    <div>
        <img src="<?= $web ?>/images/mall-m/apply-for-institution-2.jpg" style="display: block;width: 16rem">
    </div>

    <div class="await-window" style="display: none;">
        <div class="window-content">
            <img
                src=""/>
            <p>正在申请机构</p>
        </div>
        <div class="shade"></div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $("#btnSubmit").click(function () {
            var data = {};
            var isContinue = true;

            var txtName = $("#txtName").val();
            data.txtName = txtName;
            if (txtName == "") {
                alert("企业全称不能为空");
                $("#txtName").focus();
                return false;
            }

            var txtContacter = $("#txtContacter").val();
            data.txtContacter = txtContacter;
            if (txtContacter == "") {
                alert("联系人不能为空");
                $("#txtContacter").focus();
                return false;
            }

            var txtPhone = $("#txtPhone").val();
            data.txtContacterPhone = txtPhone;
            if (txtPhone == "") {
                alert("联系手机不能为空");
                $("#txtPhone").focus();
                return false;
            }

            var txtVerificationCode = $("#txtVerificationCode").val();
            data.txtVerificationCode = txtVerificationCode;
            if (txtVerificationCode == "") {
                alert("验证码不能为空");
                $("#txtVerificationCode").focus();
                return false;
            }

            var txtAddress = $("#txtAddress").val();
            data.txtAddress = txtAddress;
            if (txtAddress == "") {
                alert("联系地址不能为空");
                $("#txtAddress").focus();
                return false;
            }

            //判断手机号码是否已经注册
            var dataPhone = {};
            dataPhone.txtContacterPhone = txtPhone;
            requestJsonMallM('is-register-phone-handler', dataPhone, false, function (response) {
                if (response.status == 0) {
                    if (response.data != null) {
                        isContinue = false;
                        if (confirm("该手机号已经注册过，确定要跳转到该手机号的机构页面？")) {
                            window.location.replace('apply-for-institution-hy-success?account=' + response.data["account"] + "&record_id=" + response.data["record_id"] + "&institution_code=" + response.data["institution_code"]);
                            return false;
                        }
                    }
                }
            });

            if (!isContinue) {
                return false;
            }

            $(".await-window").show();
            requestJsonMallM('apply-for-institution-hy-form', data, true, function (res) {
                if (res.status == 0) {
                    window.location.replace('apply-for-institution-hy-success?account=' + res.data["account"] + "&password=" + res.data["password"] + "&record_id=" + res.data["record_id"] + "&institution_code=" + res.data["institution_code"]);
                } else {
                    $(".await-window").hide();
                    alert(res.message);
                    return false;
                }
            });
        });
    });
</script>